package com.api.oa.services.impl;

import cn.hutool.core.date.DateUtil;
import com.api.oa.common.UserType;
import com.api.oa.config.UserContext;
import com.api.oa.mapper.YQRequestAssMapper;
import com.api.oa.model.entity.YQRequestAss;
import com.api.oa.model.vo.OaOrgTreeVO;
import com.api.oa.services.YQRequestAssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;


@Service
public class YQRequestAssServiceImpl implements YQRequestAssService {

    @Autowired
    private YQRequestAssMapper mapper;


    @Override
    public Integer add(YQRequestAss entity) {
        Integer res = 0;
        entity.setCreatedAt(DateUtil.date());
        if (UserContext.getRole() == UserType.INS) {
            YQRequestAss oneByOrder = mapper.findOneByOrder(entity.getOrderCode(), UserType.INS_ASS);
            entity.setUserRole(UserType.INS_ASS);
            if(oneByOrder == null){
               res = mapper.insert(entity);
            }else{
                res =  mapper.update(entity);
            }
        } else if (UserContext.getRole() == UserType.GOV) {
            YQRequestAss oneByOrder = mapper.findOneByOrder(entity.getOrderCode(), UserType.GOV_ASS);
            entity.setUserRole(UserType.GOV_ASS);
            if(oneByOrder == null){
                res =  mapper.insert(entity);
            }else{
                res =  mapper.update(entity);
            }
        }

        return res;
    }

    @Override
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public Integer update(YQRequestAss entity) {
        return mapper.update(entity);
    }

    @Override
    public PageInfo<YQRequestAss> list(Long pid, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("createdAt desc,orderCode desc");
        return new PageInfo<>(mapper.list(pid));
    }

    @Override
    public YQRequestAss one(Long id) {
        return mapper.findOne(id);
    }


}
